Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9주차 미션 / 서버 3조 정소은 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ssilver01
Copy link
Member

@ssilver01 ssilver01 commented May 30, 2024

  • 인가 과정, 즉 JWT를 통한 검증이 자동으로 진행 구현
  • 페이징 처리 구현

토큰에 포함된 이메일을 사용해 userId를 찾아 반환하는 로직을 구현했습니다.
이를 통해 사용자와 관련된 API 경로에서 userId를 제거하고, 더 간결한 경로를 사용할 수 있게 되었습니다.
Spring Security의 PreAuthorize 애노테이션과 커스텀 PreAuthorize 애노테이션의 혼동을 방지하기 위해, 커스텀 애노테이션의 이름을 변경했습니다.
페이징 처리 구현은 데이터가 적어 익숙한 offset방식으로 구현했습니다. limit은 한 번에 가져올 데이터의 양을 지정하고,offset은 시작 위치를 지정합니다.

@ssilver01 ssilver01 changed the title 7주차 미션 / 서버 3조 정소은 9주차 미션 / 서버 3조 정소은 May 30, 2024
Copy link
Contributor

@jaeuk520 jaeuk520 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

미션 수행하시느라 고생하셨습니다:)
미션을 잘 수행해주셔서 코멘트를 조금 남겨드렸어요!!
InterceptorArgumentResolver가 가지는 역할/책임과 이 두가지를 빼고 구현하였을 때를 생각해보시면 아마 이번주차 내용이 더 잘 이해되실 것 같아요 ㅎㅎ
(아마 토큰을 검증하고 payload를 추출하는 로직이 인가가 필요한 모든 컨트롤러에서 중복 된다는 문제가 발생할거에요)

jwtAuthInterceptor.validateAccessToken(accessToken);
String email = jwtAuthInterceptor.getEmail(accessToken);
jwtAuthInterceptor.validatePayload(email);
long userId = authService.getUserIdByEmail(email);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

토큰을 생성할 때부터 payload에 userId를 담아 생성한다면 ArgumentResolver에서 Service를 호출하는 행위를 방지할 수 있을 것 같아요:) 레이어드 아키텍처의 의존 방향을 생각해보시면 좋을 것 같습니다! (사실 9주차 미션 제공 코드에는 이미 payload에 userId를 담아둡니다..!! 서비스 단으로 갈 필요 없이 바로 추출해서 사용하면 돼요)

@@ -25,6 +36,15 @@ public boolean supportsParameter(MethodParameter parameter) {
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
log.info("userId={}", request.getAttribute("userId"));
return request.getAttribute("userId");
String accessToken = jwtAuthInterceptor.resolveAccessToken(request);
jwtAuthInterceptor.validateAccessToken(accessToken);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

토큰에 대한 검증은 이미 Interceptor에서 끝났기 때문에 ArgumentResolver에는 요청 헤더 -> 토큰 -> payload 추출 로직만 있어도 됩니다!

.sessionManagement((sessionManagement) ->
sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
);
.csrf().disable()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'csrf()' is deprecated since version 6.1 and marked for removal
해당 방식이 6.1 버전부터 deprecated 되어 이전 코드 대로 작성해주시는게 좋을 것 같아요 :)


sql += " limit :limit offset :offset";
param.put("limit", limit);
param.put("offset", offset);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배달의 민족 앱 같은 경우에는 무한 스크롤 방식을 사용합니다. 무한 스크롤 방식은 offset보다는 lastId를 활용한 non-offset 방식을 활용해요!
해당 방식을 사용하는 것도 한번 고민해보시면 좋을 것 같아요:) 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants